package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.business.common.Constants;
import com.lvy.hczn.front.business.mapper.GoodsBoxMapper;
import com.lvy.hczn.front.business.service.*;
import com.lvy.hczn.front.common.constant.RedisConstants;
import com.lvy.hczn.front.common.core.domain.BaseModel;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.ShiroUtils;
import com.lvy.hczn.front.system.domain.business.*;
import com.lvy.hczn.front.business.mapper.SpuSortRecordMapper;
import com.lvy.hczn.front.system.mapper.SpuMapper;
import com.lvy.hczn.front.system.service.TemperatureZoneService;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 分拣记录，如报损，降级
 *
 * @author yw
 * @date 2022-08-23 11:48:09
 **/
@Slf4j
@Service
public class SpuSortRecordServiceImpl extends ServiceImpl<SpuSortRecordMapper, SpuSortRecord> implements SpuSortRecordService {

    @Autowired
    private SpuSortRecordMapper spuSortRecordMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private GoodsBoxService goodsBoxService;

    @Autowired
    private GoodsBoxDetailService goodsBoxDetailService;

    @Autowired
    private WarehousingEntryService warehousingEntryService;

    @Autowired
    private SaleOrderDetailService saleOrderDetailService;

    @Autowired
    private SaleOrderService saleOrderService;

    @Autowired
    private TemperatureZoneService temperatureZoneService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 货箱临期列表
     *
     * @param paramInfo
     * @return java.util.List<com.lvy.hczn.front.system.domain.business.SpuSortRecord>
     * @author yw
     * @date 2023-09-16 20:16:32
     */
    @Override
    public List<SpuSortRecord> untreatedList(ParamInfo paramInfo) {
        if (paramInfo == null || paramInfo.getData() == null) {
            throw new UtilException("参数为空");
        }
        SpuSortRecord spuSortRecord = JSON.parseObject(JSON.toJSONString(paramInfo.getData()), SpuSortRecord.class);
        List<SpuSortRecord> list = spuSortRecordMapper.selectListInfo(spuSortRecord);
        list.forEach(item -> {
            Spu spu = spuMapper.selectById(item.getSpuId());
            if (spu != null) {
                item.setSpuCode(spu.getSpuCode());
                item.setSpuName(spu.getName());
                TemperatureZone temperatureZone = temperatureZoneService.getOne(Wrappers.<TemperatureZone>lambdaQuery().eq(TemperatureZone::getTemperatureZoneCode, spu.getTemperatureZoneCode()).last("limit 1"));
                if (temperatureZone != null) {
                    item.setTemperatureZone(temperatureZone.getName() + "(" + temperatureZone.getTemperatureZoneCode() + ")");
                }
            }

        });
        return list;
    }

    /**
     * 货箱临期首页统计
     *
     * @param paramInfo
     * @return
     */
    @Override
    public List<SpuSortRecord> untreatedCount(ParamInfo paramInfo) {
        if (paramInfo == null || paramInfo.getData() == null) {
            throw new UtilException("参数为空");
        }
        SpuSortRecord spuSortRecord = JSON.parseObject(JSON.toJSONString(paramInfo.getData()), SpuSortRecord.class);
        if (StrUtil.isEmpty(spuSortRecord.getDataType())) {
            throw new UtilException("数据类型为空");
        }
        return spuSortRecordMapper.untreatedCount(spuSortRecord);
    }

    /**
     * 分拣前先进行临期报损操作
     *
     * @param
     * @return com.lvy.hczn.mq.rocket.ParamInfo
     * @author yw
     * @date 2023-12-23 16:29:26
     */
    public ParamInfo checkUntreatedCount() {
        SpuSortRecord spuSortRecord = new SpuSortRecord();
        spuSortRecord.setStatus(1);
        spuSortRecord.setType("1");
        spuSortRecord.setTreated(false);
        spuSortRecord.setDataType(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_FIVE);
        ParamInfo paramInfo = new ParamInfo<>();
        paramInfo.setData(spuSortRecord);
        return paramInfo;
    }

    /**
     * 条件查询
     *
     * @param spuSortRecord
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
     * @author yw
     * @date 2023-10-05 20:19:26
     */
    public LambdaQueryWrapper queryCondition(SpuSortRecord spuSortRecord) {

        LambdaQueryWrapper<SpuSortRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isEmpty(spuSortRecord.getSpuId())) {
            queryWrapper.eq(SpuSortRecord::getSpuId, spuSortRecord.getSpuId());
        }
        if (!StrUtil.isEmpty(spuSortRecord.getSkuCode())) {
            queryWrapper.eq(SpuSortRecord::getSkuCode, spuSortRecord.getSkuCode());
        }
        if (spuSortRecord.getProductDate() != null) {
            queryWrapper.le(SpuSortRecord::getProductDate, spuSortRecord.getProductDate());
        }
        if (!StrUtil.isEmpty(spuSortRecord.getBoxCode())) {
            queryWrapper.eq(SpuSortRecord::getBoxCode, spuSortRecord.getBoxCode());
        }
        if (spuSortRecord.getTreated() != null) {
            queryWrapper.eq(SpuSortRecord::getTreated, spuSortRecord.getTreated());
        }
        if (!StrUtil.isEmpty(spuSortRecord.getType())) {
            queryWrapper.eq(SpuSortRecord::getType, spuSortRecord.getType());
        }
        if (spuSortRecord.getStatus() != null) {
            queryWrapper.eq(SpuSortRecord::getStatus, spuSortRecord.getStatus());
        }
        if (!StrUtil.isEmpty(spuSortRecord.getDataType())) {
            queryWrapper.eq(SpuSortRecord::getDataType, spuSortRecord.getDataType());
        }
        return queryWrapper;
    }

    /**
     * 平台临期报损出库
     *
     * @param paramInfo
     * @return void
     * @author yw
     * @date 2023-09-18 20:36:15
     */
    @Override
    @Transactional
    public void frmLoss(ParamInfo paramInfo) throws Exception {
        if (paramInfo == null || paramInfo.getData() == null) {
            throw new UtilException("参数为空");
        }
        List<GoodsBoxDetail> goodsBoxDetails = new ArrayList<>();
        //货箱编码为空（即仓库未下架货箱），有两种情况，一种是仓库库存已清零，另一种是未入库，则从出入库单查
        boolean exist = true;
        /*大件商品，是所有货箱详情一起处理，货架的商品，是按单个货箱处理**/
        if (paramInfo.getBigProduct().equals(com.lvy.hczn.front.common.constant.Constants.GOODS_COLLECT_YES)) {
            goodsBoxDetails = bigProductFrmLoss(paramInfo);
        }
        //大件查询会为空，最好是少于一半的时候同步库存到中台，但是不好沟通确认中台库存，暂时以临期处理
        if (goodsBoxDetails.isEmpty()) {
            GoodsBoxDetail goodsBoxDetail = JSON.parseObject(JSON.toJSONString(paramInfo.getData()), GoodsBoxDetail.class);
            if (StrUtil.isEmpty(goodsBoxDetail.getId())) {
                throw new UtilException("临期报损ID为空");
            }
            //货箱编码为空（即仓库未下架货箱），有两种情况，一种是仓库库存已清零，另一种是未入库，则从出入库单查
            if (StrUtil.isEmpty(goodsBoxDetail.getBoxCode())) {
                exist = false;
                goodsBoxDetail.setProduceDate(LocalDateTimeUtil.of(goodsBoxDetail.getShelfLife()).minusDays(goodsBoxDetail.getWarrantyDate()));
//                throw new UtilException("货箱编码为空");
            }
            goodsBoxDetails.add(goodsBoxDetail);
        }

        Warehouse warehouse = warehouseService.selectLocalWarehouseFront();
        if (warehouse == null) {
            throw new UtilException("缓存仓库信息为空");
        }

        for (GoodsBoxDetail goodsBoxDetail : goodsBoxDetails) {
            WarehousingEntry warehousingEntry = new WarehousingEntry();

            /**获取到位货箱过期商品信息**/
            ParamInfo param = new ParamInfo();
            SpuSortRecord record = new SpuSortRecord();
            record.setStatus(com.lvy.hczn.front.common.constant.Constants.FRM_LOSS_DATA_TYPE_ONE);
            record.setType(com.lvy.hczn.front.business.common.Constants.SPU_SORT_RECORD_TYPE_ONE);
            record.setTreated(false);
            record.setSpuId(goodsBoxDetail.getSpuId());
            record.setSkuCode(goodsBoxDetail.getSkuCode());
            record.setProductDate(goodsBoxDetail.getProduceDate());
            record.setDataType(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_FIVE);
            param.setData(record);
            List<SpuSortRecord> spuSortRecordList = untreatedList(param);

            /**商品处理到出入库单，**/
            Integer quantity = 0;
            BigDecimal weight = BigDecimal.ZERO;
            log.error("平台临期报损出库:{}", JSON.toJSONString(spuSortRecordList));
            for (SpuSortRecord spuSortRecord : spuSortRecordList) {
                if (spuSortRecord.getProductDate().compareTo(goodsBoxDetail.getProduceDate()) == 0) {
                    //报损数量与货箱实际数量对比
                    Integer num = spuSortRecord.getQuantity() - goodsBoxDetail.getQuantity();
                    //懒得加时间报损数量字段，先这样用着
                    spuSortRecord.setRemark("总报损数量：" + spuSortRecord.getQuantity());
                    if (num >= 0) {
                        //到位的货箱报损的数量小于实际需要报损的数量，则清空货箱详情
                        quantity = quantity + goodsBoxDetail.getQuantity();
                        weight = weight.add(goodsBoxDetail.getWeight());
                        spuSortRecord.setQuantity(num);
                    } else {
                        quantity = spuSortRecord.getQuantity();
                        weight = spuSortRecord.getWeight();
                    }

                    if (num <= 0) {
                        spuSortRecord.setTreated(true);
                    }
                    goodsBoxDetail.setWeight(BigDecimal.ZERO);
                    goodsBoxDetail.setQuantity(0);
                    goodsBoxDetail.setVolume(BigDecimal.ZERO);
                    super.updateById(spuSortRecord);
                }
            }

            if (exist) {
                if (goodsBoxDetailService.updateById(goodsBoxDetail)) {
                    //货箱详情为空时，置货箱位空箱
                    GoodsBox goodsBox = goodsBoxService.selectIncDetailByBoxCode(goodsBoxDetail.getBoxCode());
                    BigDecimal remainingWeight = goodsBox.getWeight().subtract(weight);
                    if (goodsBox.getDetailList().isEmpty() || remainingWeight.compareTo(BigDecimal.ZERO) <= 0) {
                        goodsBox.setWeight(BigDecimal.ZERO);
                        goodsBox.setVolume(BigDecimal.ZERO);
                        goodsBox.setBoxType(com.lvy.hczn.front.business.common.Constants.GOODS_BOX_TYPE_EMPTY);
                        log.error("报损frmLoss变空箱：｛｝", JSON.toJSONString(goodsBox));
                        //原料箱变空箱后，库位调整，释放库位
                        warehousingEntryService.boxShelfLocationMsgSingle(goodsBox, true);
                    } else {
                        goodsBox.setWeight(remainingWeight);
                    }
                    goodsBoxService.updateById(goodsBox);
                }
            }

            initWarehousingEntry(warehousingEntry, goodsBoxDetail, warehouse, quantity, weight);

            if (warehousingEntryService.save(warehousingEntry)) {
                warehousingEntryService.commitToPlatform(warehousingEntry, true, false);
            }
             /*if (warehousingEntryService.save(warehousingEntry) && super.updateById(record)) {
            goodsBox.getDetailList().forEach(goodsBoxDetail -> {
                if (goodsBoxDetail.getSupplierCode().equals(record.getSupplierCode()) && goodsBoxDetail.getSpuId().equals(record.getSpuId()) && goodsBoxDetail.getSkuCode().equals(record.getSkuCode())) {
                    //逻辑删除，空箱出库后再物理删除
                    goodsBoxDetailService.removeById(goodsBoxDetail.getId());
                }
            });
            //提交到平台

        }*/
        }
    }

    /**
     * @Description 大件临期，查出所有货箱详情，遍历处理
     * @Param
     * @Author yw
     * @Date 2024/6/29 15:47
     * @Return
     **/
    public List<GoodsBoxDetail> bigProductFrmLoss(ParamInfo paramInfo) {
        SpuSortRecord spuSortRecord = JSON.parseObject(JSON.toJSONString(paramInfo.getData()), SpuSortRecord.class);
        LambdaQueryWrapper<GoodsBoxDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsBoxDetail::getSpuId, spuSortRecord.getSpuId());
        queryWrapper.eq(GoodsBoxDetail::getSkuCode, spuSortRecord.getSkuCode());
        queryWrapper.eq(GoodsBoxDetail::getSpecCode, spuSortRecord.getSpecCode());
        queryWrapper.eq(GoodsBoxDetail::getSupplierCode, spuSortRecord.getSupplierCode());
        queryWrapper.eq(GoodsBoxDetail::getShelfLife, spuSortRecord.getShelfLife());
        queryWrapper.gt(GoodsBoxDetail::getWeight, BigDecimal.ZERO);
        return goodsBoxDetailService.list(queryWrapper);
    }

    /**
     * 退货入库报损,仓库报损，直接报损，不更新到平台,若是商品临期的则更新库存
     *
     * @param paramInfo
     * @return void
     * @author yw
     * @date 2023-09-28 10:03:56
     */
    @Override
    @Transactional
    public void frmLossDone(ParamInfo paramInfo) throws Exception {
        if (paramInfo == null || paramInfo.getData() == null) {
            throw new UtilException("退货报损数据不能为空");
        }
        SpuSortRecord spuSortRecord = JSON.parseObject(JSON.toJSONString(paramInfo.getData()), SpuSortRecord.class);
        if (StrUtil.isEmpty(spuSortRecord.getSaleOrderDetailId())) {
            throw new UtilException("仓库报损销售单详情ID为空");
        }

        SaleOrderDetail saleOrderDetail = saleOrderDetailService.getById(spuSortRecord.getSaleOrderDetailId());
        if (saleOrderDetail == null) {
            throw new UtilException("仓库报损销售单详情不存在");
        }

        SaleOrder saleOrder = saleOrderService.getOne(Wrappers.<SaleOrder>lambdaQuery().eq(SaleOrder::getSaleOrderCode, saleOrderDetail.getSaleOrderCode()).in(SaleOrder::getType, com.lvy.hczn.front.common.constant.Constants.SALE_ORDER_TYPE_TWO, com.lvy.hczn.front.common.constant.Constants.SALE_ORDER_TYPE_FOUR).orderByAsc(BaseModel::getCreateTime).last("limit 1"));

        if (saleOrder == null) {
            throw new UtilException("仓库报损销售单不存在");
        }

        //查询销售分拣出库单据
        WarehousingEntry entry;
        if (!StrUtil.isEmpty(saleOrder.getMemberCode())) {
            entry = warehousingEntryService.getOne(Wrappers.<WarehousingEntry>lambdaQuery().eq(WarehousingEntry::getSpuId, saleOrderDetail.getSpuId()).eq(WarehousingEntry::getSkuCode, saleOrderDetail.getSkuCode()).eq(WarehousingEntry::getMemberCode, saleOrder.getMemberCode()).in(WarehousingEntry::getDataType, com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_EIGHT, com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_ELEVEN).orderByDesc(BaseModel::getCreateTime).last("limit 1"));
        } else {
            entry = warehousingEntryService.getOne(Wrappers.<WarehousingEntry>lambdaQuery().eq(WarehousingEntry::getSpuId, saleOrderDetail.getSpuId()).eq(WarehousingEntry::getSkuCode, saleOrderDetail.getSkuCode()).in(WarehousingEntry::getDataType, com.lvy.hczn.front.common.constant.Constants.entryDataTypeList()).orderByDesc(BaseModel::getCreateTime).last("limit 1"));
        }

        if (entry == null) {
            throw new UtilException("仓库报损，单据不存在");
        }
        SpuSortRecord record = new SpuSortRecord();
        record.setRecodeCode(IdUtil.getSnowflakeNextIdStr());
        record.setStatus(com.lvy.hczn.front.common.constant.Constants.FRM_LOSS_DATA_TYPE_ZERO);
        record.setType(com.lvy.hczn.front.business.common.Constants.SPU_SORT_RECORD_TYPE_ONE);
        record.setTreated(true);
        record.setBoxCode("0");
        record.setSpuId(saleOrderDetail.getSpuId());
        record.setSkuCode(saleOrderDetail.getSkuCode());
        record.setProductDate(entry.getPickTime());
        record.setWarrantyDate(entry.getWarrantyDate());
        record.setSupplierCode(entry.getSupplierCode());
        record.setProducerId(entry.getProducerId());
        record.setQuantity(saleOrderDetail.getQuantity());
        record.setWeight(saleOrderDetail.getWeight());
        record.setVolume(saleOrderDetail.getVolume());
        record.setClientId(ShiroUtils.getLoginName());
        record.setDataType(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_TEN);
        if (super.save(record)) {
            /**退货入库，有临期的，则减掉临期重量和数量，且更新到平台**/
            syncStockIfFrmLoss(record, entry);

            saleOrderDetail.setStatus(com.lvy.hczn.front.common.constant.Constants.SALE_ORDER_DETAIL_STATUS_SIX);
            saleOrderDetailService.updateById(saleOrderDetail);
        }
    }

    /**
     * 退货入库报损时，若商品有临期的，则更新临期库存，且同步到平台
     *
     * @param record
     * @return void
     * @author yw
     * @date 2023-11-06 16:27:00
     */
    public void syncStockIfFrmLoss(SpuSortRecord record, WarehousingEntry entry) {
        String dataType = com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_FIVE;
        record.setDataType(dataType);
//        SpuSortRecord sortRecord = super.getOne(Wrappers.<SpuSortRecord>lambdaQuery().eq(SpuSortRecord::getDataType, dataType).eq(SpuSortRecord::getSpuId, record.getSpuId()).eq(SpuSortRecord::getSkuCode, record.getSkuCode()).eq(SpuSortRecord::getTreated, false).orderByDesc(BaseModel::getCreateTime).last("limit 1"));
        record.setTreated(false);
        SpuSortRecord sortRecord = spuSortRecordMapper.selectObjectInfo(record);
        if (sortRecord != null) {
            sortRecord.setWeight(sortRecord.getWeight().subtract(record.getWeight()));
            sortRecord.setQuantity(sortRecord.getQuantity() - record.getQuantity());
            if (super.updateById(sortRecord)) {
                Warehouse warehouse = redisCache.getCacheObject(RedisConstants.WAREHOUSE_WMS);
                if (warehouse == null) {
                    throw new UtilException("缓存内请先配置仓库信息");
                }
                WarehousingEntry warehouseEntry = new WarehousingEntry();
                warehouseEntry.setEntryCode(entry.getBatchCode());
                warehouseEntry.setServiceCode(warehouseEntry.getEntryCode());
                warehouseEntry.setServiceType(com.lvy.hczn.front.common.constant.Constants.WAREHOUSE_ENTRY_OUT_TYPE_SEVEN);
                warehouseEntry.setDataType(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_TEN);
                warehouseEntry.setWarehouseCode(warehouse.getWarehouseCode());
                warehouseEntry.setBoxCode(entry.getBoxCode());
                warehouseEntry.setSpuId(entry.getSpuId());
                warehouseEntry.setSkuCode(entry.getSkuCode());
                warehouseEntry.setSpecCode(entry.getSpecCode());
                warehouseEntry.setBatchCode(warehouseEntry.getEntryCode());
                warehouseEntry.setPrice(BigDecimal.ZERO);
                warehouseEntry.setQuantity(record.getQuantity());
                warehouseEntry.setWeight(record.getWeight());
                warehouseEntry.setWeightDone(true);
                warehouseEntry.setUnit(entry.getUnit());
                warehouseEntry.setWeightOfPerBox(BigDecimal.ZERO);
                warehouseEntry.setNumberOfPerBox(0);
                warehouseEntry.setSupplierCode(entry.getSupplierCode());
                warehouseEntry.setProducerId(entry.getProducerId());
                warehouseEntry.setWarrantyDate(entry.getWarrantyDate());
                warehouseEntry.setPickTime(entry.getPickTime());
                if (warehouseEntry.getPickTime() != null && warehouseEntry.getWarrantyDate() != null) {
                    warehouseEntry.setShelfLife(warehouseEntry.getPickTime().plusDays(warehouseEntry.getWarrantyDate()));
                }
                warehouseEntry.setStatus(Constants.ENTRY_ING);
                warehouseEntry.setSourceId(warehouse.getWarehouseCode());
                if (warehousingEntryService.save(warehouseEntry)) {
                    warehousingEntryService.commitToPlatform(warehouseEntry, true, false);
                }
            }
        }
    }

    public void initWarehousingEntry(WarehousingEntry warehousingEntry, GoodsBoxDetail goodsBoxDetail, Warehouse warehouse, Integer quantity, BigDecimal weight) {
        List<String> dataTypes = new ArrayList<>();
        dataTypes.add(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_ONE);
        dataTypes.add(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_TWO);
        dataTypes.add(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_THREE);
        dataTypes.add(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_FOUR);
        dataTypes.add(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_TWELVE);
        //出库时会清空货箱详情，根据入库单查询
        WarehousingEntry entry = warehousingEntryService.getOne(Wrappers.<WarehousingEntry>lambdaQuery().eq(WarehousingEntry::getPickTime, goodsBoxDetail.getProduceDate()).eq(WarehousingEntry::getWarrantyDate, goodsBoxDetail.getWarrantyDate()).eq(WarehousingEntry::getSpuId, goodsBoxDetail.getSpuId()).eq(WarehousingEntry::getSkuCode, goodsBoxDetail.getSkuCode()).in(WarehousingEntry::getDataType, dataTypes).orderByDesc(BaseModel::getCreateTime).last("limit 1"));
        if (entry == null) {
            throw new UtilException("入库信息不存在");
        }

        warehousingEntry.setEntryCode(entry.getEntryCode());
        warehousingEntry.setServiceCode(entry.getServiceCode());
        warehousingEntry.setBoxCode(entry.getBoxCode());
        warehousingEntry.setDataType(com.lvy.hczn.front.common.constant.Constants.WAREHOUSING_ENTRY_DATA_TYPE_FIVE);
        warehousingEntry.setSourceId(entry.getSourceId());

        warehousingEntry.setWarehouseCode(warehouse.getWarehouseCode());
        warehousingEntry.setSpuId(entry.getSpuId());
        warehousingEntry.setSkuCode(entry.getSkuCode());
        warehousingEntry.setSpecCode(entry.getSpecCode());
        warehousingEntry.setBatchCode(entry.getEntryCode());
        warehousingEntry.setQuantity(quantity);
        warehousingEntry.setWeight(weight);
        warehousingEntry.setUnit(entry.getUnit());
        warehousingEntry.setSupplierCode(entry.getSupplierCode());
        warehousingEntry.setProducerId(entry.getProducerId());
        warehousingEntry.setWarrantyDate(entry.getWarrantyDate());
        warehousingEntry.setPickTime(entry.getPickTime());
        warehousingEntry.setStatus(Constants.ENTRY_ING);
    }

    /**
     * 分拣点击缺货时，过滤已临期的商品
     *
     * @param sortTaskDetailInfo
     * @return
     */
    @Override
    public SpuSortRecord selectSortFrmLossBySpu(SortTaskDetail sortTaskDetailInfo) {
        if (StrUtil.hasEmpty(sortTaskDetailInfo.getSpuId(), sortTaskDetailInfo.getSkuCode())) {
            throw new UtilException("商品编码或SKU为空");
        }
        return spuSortRecordMapper.selectSortFrmLossBySpu(sortTaskDetailInfo);
    }

    @Override
    public SpuSortRecord selectByRecodeCode(SpuSortRecord sortRecord) {
        if (StrUtil.isEmpty(sortRecord.getRecodeCode())) {
            throw new UtilException("参数为空");
        }
        return spuSortRecordMapper.selectByRecodeCode(sortRecord);
    }

}
